package com.cxl.controller;


import com.cxl.entity.Unit;
import com.cxl.service.IUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 崔学磊
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUnitService iUnitService;

    @GetMapping(value = "/getunitlist")
    public List<Unit> getunitlist() {
        List<Unit> units = null;
        if (redisTemplate.hasKey("units")) {
            units = redisTemplate.opsForList().range("units", 0, -1);
        } else {
            units = iUnitService.list();
            redisTemplate.opsForList().rightPushAll("units", units);
        }
        return units;
    }

}
